package com.ldoublem.loadingviewlib;


import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;


public class LVLineWithText extends Component implements Component.EstimateSizeListener, Component.DrawTask {

    private Paint mPaintBar;
    private Paint mPaintText;


    private float mWidth = 0f;
    private float mHigh = 0f;
    private int mVlaue = 0;

    private float mPadding = 5f;

    public LVLineWithText(Context context) {
        this(context, null);
    }

    public LVLineWithText(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    public LVLineWithText(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }


    private void initPaint() {
        setEstimateSizeListener(this);
        addDrawTask(this);
        mPaintBar = new Paint();
        mPaintBar.setAntiAlias(true);
        mPaintBar.setStyle(Paint.Style.FILL_STYLE);
        mPaintBar.setColor(Color.WHITE);
        mPaintBar.setTextSize(dip2px(10));
        mPaintBar.setStrokeWidth(dip2px(1));
        mPaintText = new Paint();
        mPaintText.setAntiAlias(true);
        mPaintText.setStyle(Paint.Style.FILL_STYLE);
        mPaintText.setColor(Color.WHITE);
        mPaintText.setTextSize(dip2px(10));
        mPaintText.setStrokeWidth(dip2px(1));


    }

    public void setTextColor(int color) {
        mPaintText.setColor(new Color(color));
        invalidate();
    }

    public void setViewColor(int color) {
        mPaintBar.setColor(new Color(color));
        invalidate();
    }


    public float getFontlength(Paint paint, String str) {

        return paint.getTextBounds(str).getWidth();
    }

    public float getFontHeight(Paint paint, String str) {

        return paint.getTextBounds(str).getHeight();

    }

    public int dip2px(float dpValue) {
        final float scale = AttrHelper.getDensity(getContext());
        return (int) (dpValue * scale + 0.5f);
    }

    public void setValue(int value) {
        this.mVlaue = value;
        invalidate();
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        String text = mVlaue + "%";
        float textlength = getFontlength(mPaintText, text);
        float texthigh = getFontHeight(mPaintText, text);

        if (mVlaue == 0) {
            canvas.drawText(mPaintText,text, mPadding, mHigh / 2 + texthigh / 2 );
            canvas.drawLine(new Point(mPadding + textlength, mHigh / 2),
                    new Point(mWidth - mPadding, mHigh / 2), mPaintBar);


        } else if (mVlaue >= 100) {
            canvas.drawText(mPaintText,text, mWidth - mPadding - textlength, mHigh / 2 + texthigh / 2);
            canvas.drawLine(new Point(mPadding, mHigh / 2),
                    new Point(mWidth - mPadding - textlength, mHigh / 2), mPaintBar);

        } else {
            float w = mWidth - 2 * mPadding - textlength;
            canvas.drawLine(new Point(mPadding, mHigh / 2), new Point(mPadding + w * mVlaue / 100,
                    mHigh / 2), mPaintBar);
            canvas.drawLine(new Point(mPadding + w * mVlaue / 100 + textlength, mHigh / 2),
                    new Point(mWidth - mPadding, mHigh / 2), mPaintBar);
            canvas.drawText(mPaintText,text, mPadding + w * mVlaue / 100, mHigh / 2 + texthigh / 2 );

        }
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {
        mWidth = EstimateSpec.getSize(i);
        mHigh = EstimateSpec.getSize(i1);
        return false;
    }
}
